<?php
/**
 * 前端用户账户信息服务
 * Created by PhpStorm.
 * User: 
 * Date: 
 * Time: 
 */

namespace app\services\controller;

use think\captcha\Captcha;
use think\Config;
use think\Db;
use think\Request;
use think\Response;
use weixin\weixin;
use app\model\UserBase;
use ethink\mvc\BaseController;
use ethink\acl\Auth;
use think\Loader;

class Statistic extends BaseController
{
    public function __construct(Request $request = null)
    {
        $this->auth = new Auth(Config::get('api')['auth']);
        parent::__construct($request);
    }
 

     /**
     * [病例列表，所有的
     * ]
     * @return [type] [description]
     */
    public function caseAllList(){
         if($this->request->isGet()){
            $reqData = $this->request->get();
            trace("case-caseAllList,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            //1校验登陆状态
            $user = $this->checkUserStatus();  
            if($user['status']){
                return $this->responseJson(null, $user['status']);                
            }
            $userId = $user['session']['userId'];
            $reqData['roleId'] = $user['session']['roleId'];
            //2查询数据
            $Model = new \app\services\model\CaseList();
            $reqData['pageSize'] = Config::get('api')['list']['page_size'];
            $list = $Model->caseAllList($userId,$reqData);
            if($list['status'])
                return $this->responseJson(null, $list['status']);
            //
            $ret['data'] = $list['info']->toArray();
            return $this->responseJson($ret, 0);                      
        }      
    } 

    /**
     * [病例的变更记录
     * ]
     * @return [type] [description]
     */
    public function caseChangeList(){
         if($this->request->isGet()){
            $reqData = $this->request->get();
            trace("case-caseChangeList,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            //1校验登陆状态
            $user = $this->checkUserStatus();  
            if($user['status']){
                return $this->responseJson(null, $user['status']);                
            }
            $userId = $user['session']['userId'];
            $reqData['roleId'] = $user['session']['roleId'];
            if(empty($reqData['roleId'])){
                return $this->responseJson(null, 1002);     
            }
            //2查询数据
            $Model = new \app\services\model\CaseList();
            $reqData['pageSize'] = 10000;//Config::get('api')['list']['page_size']; 暂时页面不需要分页，
            $list = $Model->caseChangeList($userId,$reqData);
            $ret['data'] = $list->toArray();
            return $this->responseJson($ret, 0);                      
        }      
    } 

    /**
     * [病例的状态变化
     * ]
     * @return [type] [description]
     */
    public function caseLog(){
         if($this->request->isGet()){
            $reqData = $this->request->get();
            trace("case-caseLog,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            //1校验登陆状态
            $user = $this->checkUserStatus();  
            if($user['status']){
                return $this->responseJson(null, $user['status']);                
            }
            $userId = $user['session']['userId'];
            $reqData['roleId'] = $user['session']['roleId'];
            if(empty($reqData['roleId'])){
                return $this->responseJson(null, 1002);     
            }
            //2查询数据
            $Model = new \app\services\model\CaseList();
            $reqData['pageSize'] = Config::get('api')['list']['page_size'];
            $list = $Model->caseLog($userId,$reqData);
            $ret['data'] = $list;
            return $this->responseJson($ret, 0);                      
        }      
    }

    /**
     * [质量核查首页]
     * @return [type] [description]
     */
    public function qualityIndex(){
         if($this->request->isGet()){
            $reqData = $this->request->get();
            trace("case-qualityIndex,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            //1校验登陆状态
            $user = $this->checkUserStatus();  
            if($user['status']){
                return $this->responseJson(null, $user['status']);                
            }
            $userId = $user['session']['userId'];
            $reqData['roleId'] = $user['session']['roleId'];
            //2查询数据
            $Model = new \app\services\model\CaseList();
            $reqData['pageSize'] = Config::get('api')['list']['page_size'];
            $list = $Model->caseQualityIndex($userId,$reqData);
            // var_dump($list);exit;
            $ret = $list;
            return $this->responseJson($ret, 0);                      
        }      
    }  

    /**
     * [质量报告中的四个饼图
     *  //曹宇:空数据:值为空的字段占总字段的比例   异常数据：被质疑过的均为异常数据与总字段比例 有效数据：没有被质疑过且不为空的数据
        //1总字段数
        //
        //2没有修改过的字段
        //
        //3修改多的字段
        //
        //4空字段个数
        //
        //5质疑类型，前几个+其他
        ]
     * @return [type] [description]
     */
    public function qualityOther(){
         if($this->request->isGet()){
            $reqData = $this->request->get();
            trace("case-qualityOther,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            //1校验登陆状态
            $user = $this->checkUserStatus();  
            if($user['status']){
                return $this->responseJson(null, $user['status']);                
            }
            $userId = $user['session']['userId'];
            $reqData['roleId'] = $user['session']['roleId'];
            //2查询数据
            $Model = new \app\services\model\CaseList(); 
            // var_dump($reqData);exit;
            $list = $Model->caseQualityOther($userId,$reqData);
            if($list['status'])
                return $this->responseJson(null,$list['status']);     
            //3查询所有变更记录
            $reqData['pageSize'] = 10000;
            $listChange = $Model->caseChangeList($userId,$reqData);
            $list['data']['changeLog'] = $listChange->toArray();
            //4列表
            $Model = new \app\services\model\CaseList();
            $reqData['pageSize'] = Config::get('api')['list']['page_size'];
            $listret = $Model->caseQualityIndex($userId,$reqData);
            foreach ($listret['data'] as $key => $value) {
                $tem['count'] = $list['out_data']['emptyColumnCount'];
                $tem['columnCount'] = $list['out_data']['emptyColumnCount'];
                $tem['status'] = 20; // 为空
                array_push($listret['data'][$key]['data'],$tem);
            }
            $list['data']['list'] = $listret['data'];
                             
            return $this->responseJson($list, 0);                      
        }      
    } 
    

    /**
     * 导出一个页面
     * @return [type] [description]
     */
    public function exportResult(){
         if($this->request->isGet()){
            $reqData = $this->request->get();
            trace("case-exportResult,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            // 1校验登陆状态
            $user = $this->checkUserStatus();  
            if($user['status']){
                return $this->responseJson(null, $user['status']);                
            }
            $userId = $user['session']['userId'];
            // $reqData['roleId'] = $user['session']['roleId'];
            // 1.实例化TCPDF对象
            Vendor('tecnickcom.tcpdf.tcpdf');
            $pdf = new \TCPDF("Portrait", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
            // 2.设置标题关键字等（步骤可以省略）
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('Nicola Asuni');
            $pdf->SetTitle('TCPDF演示');
            $pdf->SetSubject('TCPDF Tutorial');
            $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
            // 3.设置字体(这步很重要不然中文乱码)
            $fontFile = dirname(dirname(dirname(dirname(__FILE__)))).'/vendor/DroidSansFallback.ttf';
            $fontname = \TCPDF_FONTS::addTTFfont($fontFile, 'TrueTypeUnicode', '', 32);
            $pdf->SetFont($fontname, '', 20);
            // 4.添加个页面
            $pdf->AddPage();
            // 5.将HTML生成PDF
            $html = "<html>
                        <head><title>test</title></head>
                        <body>
                            测试题
                        <table>
                            <tr><th>key</th><th>key2</th></tr>
                            <tr><td>value</td><td>value2</td></tr>
                            <tr><td>vv</td><td>vv2</td></tr>
                        </table></body>
                    </html>";
            // echo $html;exit;
            $pdf->writeHTML($html);
            // 6.输出（默认直接在浏览器显示）
            // echo 0;exit;
            // $pdf->Output();
            $pdf->Output('statistic.pdf','D');//这个是下载，其他方式请替换参数
                        
            return $this->responseJson(null, 0);                      
        }      
    } 

}